package android.hackathon.bp.hayaben;

import com.felicanetworks.mfc.AppInfo;
import com.felicanetworks.mfc.Felica;
import com.felicanetworks.mfc.FelicaEventListener;
import com.felicanetworks.mfc.FelicaException;
import com.felicanetworks.mfc.PushIntentSegment;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.text.SpannableStringBuilder;

public class HayabenPushActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button)findViewById(R.id.pairing);
        btn.setOnClickListener(this);
    }

	@Override
	protected void onPause() {
		super.onPause();
		FelicaService service = FelicaService.getInstance();
		try {
			service.disconnectAndInactivate();
		} catch (Exception e) {
			// Log.e(TAG, e.getMessage(), e);
			// String msg = e.toString();
			// m(msg);
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		//Felicaへ接続
		FelicaService service = FelicaService.getInstance();
		service.setContext(this, new FelicaEventListener() {

			public void finished() {
				//Buttonを有効化
				//Button.setEnabled(true);
			}

			public void errorOccurred(int arg0, String arg1, AppInfo arg2) {
				String msg;
				msg = String.format(
						"FelicaEventListener#errorOccurred(%s,%s,%s)", arg0,
						arg1, arg2);
				m(msg);
			}
		});
		try {
			service.connectAndActivate();
		} catch (Exception e) {
			// e.printStackTrace();
			// String msg = e.getMessage();
			// m(msg);
		}
	}

	void m(String text) {
		Toast t = Toast.makeText(this, text, Toast.LENGTH_SHORT);
		t.show();
	}

	void sendFelica() {

		try {
			Felica felica = FelicaService.getFelica();
			felica.open();

			try {
				Intent intent = new Intent(this, ReceiveWifiActivity.class);
				//TODO SendData
				//intent.setData(Uri.parse("AccessPoint,WEP-Key"));
				EditText text1 = (EditText)findViewById(R.id.EditText01);
				EditText text2 = (EditText)findViewById(R.id.EditText02);

				SpannableStringBuilder sb1 = (SpannableStringBuilder)text1.getText();
				SpannableStringBuilder sb2 = (SpannableStringBuilder)text2.getText();
				
				intent.setData(Uri.parse(sb1.toString() + "," + sb2.toString()));

				// Felica APIで任意のIntentを投げるためのPushIntentSegmenを投げる。
				// PushIntentSegmentで投げたIntentはFelicaのAPIによってstartActivity()で呼び出される。
				PushIntentSegment pushSegment = new PushIntentSegment(intent);
				felica.push(pushSegment);
			} finally {
				felica.close();
			}
		} catch (FelicaException e) {
			 e.printStackTrace();
			 m("FelicaEventListener.finished.FelicaException:" + e.getID());
		}
	}

	@Override
	public void onClick(View view) {
		// TODO 自動生成されたメソッド・スタブ
		sendFelica();
	}
}
